/*
 * 
 * 
 * 
 */
package com.hboxs.asl.qcloudLVB.entity;

import com.fasterxml.jackson.annotation.JsonValue;
import com.hboxs.asl.entity.*;
import com.hboxs.asl.util.DateUtils;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static javax.persistence.CascadeType.REMOVE;

/**
 * Created by xxx on 10/12/2016.
 * Entity - 直播记录
 */
@Entity
@Table(name = "xx_live")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "xx_live_sequence")
public class Live extends BaseEntity {

    private static final long serialVersionUID = 3432656497429644250L;

    /**
     * 直播标题
     */
    private String title;

    /**
     * 封面
     */
    private String cover;

    /**
     * 主播(id,头像,用户名)
     */
    private Member anchor;

    /**
     * 地址
     */
    private String address;

    /**
     * 聊天群组id
     */
    private String groupid;

    /**
     * ip
     */
    private String ip;

    /**
     * 直播时长
     */
    private Long timeSpan;

    public enum LiveStatus {
        //未直播
        nolive,
        //直播中
        live,
        //已结束
        finished,
        //已禁播
        forbidden
    }

    private Course course;

    /**
     * 播放状态
     */
    private LiveStatus liveStatus;

    /**
     * (正在)观看成员列表
     */
    private Set<Member> group = new HashSet<>();

    /**
     * 点赞成员集合
     */
    private Set<Member> admireMembers = new HashSet<>();

    /**
     * 收藏成员集合
     */
    private Set<Member> favorMembers = new HashSet<>();

    /**
     * 观看过的会员
     */
    private Set<Member> watchedMembers = new HashSet<>();

    /**
     * 观看过的机器人
     */
    private Set<Robot> watchedRobots = new HashSet<>();

    /**
     * 产品
     */
    private Set<Product> products = new HashSet<>();

    /**
     * 连麦ids集合
     */
    private Set<Long> connectedLiveIds = new HashSet<>();

    /**
     * 类型
     */
    public enum Type {

        /**
         * 主播
         */
        host,

        /**
         * 连麦
         */
        connection;

        private static Map<String, Type> map = new HashMap<>();

        static {
            map.put("主播", host);
            map.put("连麦", connection);
        }

        public static Type forValue(String value) {
            return map.get(value);
        }

        @JsonValue
        public String toValue() {
            for (Map.Entry<String, Type> entry : map.entrySet()) {
                if (entry.getValue() == this) {
                    return entry.getKey();
                }
            }
            return "";
        }
    }

    private Type type = Type.host;

    /**
     * 连麦直播id
     */
    private Long connectLiveId;

    /**
     * 回看视频url
     */
    private String videoURL;

    /**
     * 回看MP4URL
     */
    private String mp4URL;

    /**
     * 假粉数量
     */
    private Long hisRobots;

    /**
     * 获取直播标题
     *
     * @return 直播标题
     */
    public String getTitle() {
        return title;
    }

    /**
     * 设置直播标题
     *
     * @param title 直播标题
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * 获取封面
     *
     * @return 封面
     */
    public String getCover() {
        return cover;
    }

    /**
     * 设置封面
     *
     * @param cover 封面
     */
    public void setCover(String cover) {
        this.cover = cover;
    }

    /**
     * 获取主播
     *
     * @return 主播
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(nullable = false, updatable = false)
    public Member getAnchor() {
        return anchor;
    }

    /**
     * 主播
     *
     * @param anchor 主播
     */
    public void setAnchor(Member anchor) {
        this.anchor = anchor;
    }

    /**
     * 获取地址
     *
     * @return 地址
     */
    public String getAddress() {
        return address;
    }

    /**
     * 设置地址
     *
     * @param address 地址
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * 获取群组id
     *
     * @return 群组id
     */
    public String getGroupid() {
        return groupid;
    }

    /**
     * 设置群组id
     *
     * @param groupid 群组id
     */
    public void setGroupid(String groupid) {
        this.groupid = groupid;
    }

    /**
     * 获取ip
     *
     * @return ip
     */
    public String getIp() {
        return ip;
    }

    /**
     * 设置ip
     *
     * @param ip ip
     */
    public void setIp(String ip) {
        this.ip = ip;
    }

    /**
     * 获取直播时长
     *
     * @return 直播时长
     */
    public Long getTimeSpan() {
        return timeSpan;
    }

    /**
     * 设置直播时长
     *
     * @param timeSpan 直播时长
     */
    public void setTimeSpan(Long timeSpan) {
        this.timeSpan = timeSpan;
    }

    /**
     * 获取直播状态
     *
     * @return 直播状态
     */
    public LiveStatus getLiveStatus() {
        return liveStatus;
    }

    /**
     * 设置直播状态
     *
     * @param liveStatus 直播状态
     */
    public void setLiveStatus(LiveStatus liveStatus) {
        this.liveStatus = liveStatus;
    }

    /**
     * 获取群组列表
     *
     * @return 群组列表
     */
    @OneToMany(mappedBy = "watchingLive", fetch = FetchType.LAZY)
    public Set<Member> getGroup() {
        return group;
    }

    /**
     * 设置群组列表
     *
     * @param group 群组列表
     */
    @Deprecated
    public void setGroup(Set<Member> group) {
        this.group = group;
    }

    /**
     * 获取点赞会员
     *
     * @return 点赞会员
     */
    @ManyToMany(mappedBy = "admireLives", fetch = FetchType.LAZY)
    public Set<Member> getAdmireMembers() {
        return admireMembers;
    }

    /**
     * 设置点赞会员
     *
     * @param admireMembers 点赞会员
     */
    @Deprecated
    public void setAdmireMembers(Set<Member> admireMembers) {
        this.admireMembers = admireMembers;
    }

    /**
     * 获取收藏会员
     *
     * @return 收藏会员
     */
    @ManyToMany(mappedBy = "favorLives", fetch = FetchType.LAZY)
    public Set<Member> getFavorMembers() {
        return favorMembers;
    }

    /**
     * 设置收藏会员
     *
     * @param favorMembers 收藏会员
     */
    @Deprecated
    public void setFavorMembers(Set<Member> favorMembers) {
        this.favorMembers = favorMembers;
    }

    /**
     * 获取观看过的会员
     *
     * @return 观看过的会员
     */
    @ManyToMany(fetch = FetchType.LAZY)
    public Set<Member> getWatchedMembers() {
        return watchedMembers;
    }

    /**
     * 设置观看过的会员
     *
     * @param watchedMembers 观看过的会员
     */
    public void setWatchedMembers(Set<Member> watchedMembers) {
        this.watchedMembers = watchedMembers;
    }

    /**
     * 获取观看过的机器人
     *
     * @return 观看过的机器人
     */
    @ManyToMany(fetch = FetchType.LAZY)
    public Set<Robot> getWatchedRobots() {
        return watchedRobots;
    }

    public void setWatchedRobots(Set<Robot> watchedRobots) {
        this.watchedRobots = watchedRobots;
    }

    /**
     * 获取当前观看人数（包括机器人）
     *
     * @return 观看人数
     */
    @Transient
    public int getWatchCount() {
        return getGroup().size() + getWatchedRobots().size();
    }

    /**
     * 历史获取观看人数（包括机器人）
     *
     * @return 观看人数
     */
    @Transient
    public int getHisWatchCount() {
        return getWatchedMembers().size() + getWatchedRobots().size();
    }

    /**
     * 获取观看机器人
     *
     * @return 观看人数
     */
    @Transient
    public int getHisWatchRobotCount() {
        return getWatchedRobots().size();
    }

    /**
     * 获取当前观看人数（排除机器人）
     *
     * @return 观看人数
     */
    @Transient
    public int getWatchCountWithoutRobot() {
        return getGroup().size();
    }

    /**
     * 历史获取观看人数（排除机器人）
     *
     * @return 观看人数
     */
    @Transient
    public int getHisWatchCountWithoutRobot() {
        return getWatchedMembers().size();
    }

    /**
     * 获取点赞人数
     *
     * @return 点赞人数
     */
    @Transient
    public int getAdmireCount() {
        return getAdmireMembers().size();
    }

    /**
     * 获取直播时间时分秒
     */
    @Transient
    public String getTimeSpanChinese() {
        if (timeSpan == null) {
            return "";
        }
        return DateUtils.formatTime(timeSpan);
    }

    /**
     * 获取产品
     *
     * @return 产品
     */
    @ManyToMany(fetch = FetchType.LAZY)
    public Set<Product> getProducts() {
        return products;
    }

    /**
     * 设置产品
     *
     * @param products 产品
     */
    public void setProducts(Set<Product> products) {
        this.products = products;
    }

    @ElementCollection
    @CollectionTable(name = "xx_connected_live_ids")
    public Set<Long> getConnectedLiveIds() {
        return connectedLiveIds;
    }

    public void setConnectedLiveIds(Set<Long> connectedLiveIds) {
        this.connectedLiveIds = connectedLiveIds;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public Long getConnectLiveId() {
        return connectLiveId;
    }

    public void setConnectLiveId(Long connectLiveId) {
        this.connectLiveId = connectLiveId;
    }

    /**
     * 删除前处理
     */
    @PreRemove
    public void preRemove() {
        Set<Member> members = getGroup();
        if (members != null) {
            for (Member member : members) {
                member.setWatchingLive(null);
            }
        }

        Set<Member> watchedMembers = getWatchedMembers();
        if (watchedMembers != null) {
            for (Member member : watchedMembers) {
                member.getWatchedLives().remove(this);
            }
        }
    }

    @OneToOne(mappedBy = "live" ,fetch = FetchType.LAZY, cascade = REMOVE)
    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public String getVideoURL() {
        return videoURL;
    }

    public void setVideoURL(String videoURL) {
        this.videoURL = videoURL;
    }

    public String getMp4URL() {
        return mp4URL;
    }

    public void setMp4URL(String mp4URL) {
        this.mp4URL = mp4URL;
    }

    public Long getHisRobots() {
        return hisRobots;
    }

    public void setHisRobots(Long hisRobots) {
        this.hisRobots = hisRobots;
    }
}